/*
 * ACEDrawingView: https://github.com/acerbetti/ACEDrawingView
 *
 * Copyright (c) 2013 Stefano Acerbetti
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

#import "ACEDrawingTools.h"

CGPoint midPoint(CGPoint p1, CGPoint p2)
{
    return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}

#pragma mark - ACEDrawingPenTool

@implementation ACEDrawingPenTool

@synthesize firstPoint = _firstPoint;
@synthesize lastPoint = _lastPoint;
@synthesize lineColor = _lineColor;
@synthesize lineAlpha = _lineAlpha;
@synthesize mypath = _mypath;
@synthesize isSel = _isSel;//new add
@synthesize rightdownSel = _rightdownSel;
@synthesize leftdownSel = _leftdownSel;
@synthesize rightupSel = _rightupSel;
@synthesize leftupSel = _leftupSel;
@synthesize isDel = _isDel;
@synthesize original = _original;

- (id)init
{
    self = [super init];
    if (self != nil) {
        self.lineCapStyle = kCGLineCapRound;
    }
    return self;
}

- (id)Copy
{
//    ACEDrawingPenTool<ACEDrawingTool> *p = [ACEDrawingPenTool bezierPathWithCGPath:self.CGPath];
    
    /// 上面的iOS9才能用，于是改成这个
    ACEDrawingPenTool <ACEDrawingTool> *p = [[ACEDrawingPenTool alloc] init];
    p.CGPath = self.CGPath;
    
    p.firstPoint=self.firstPoint;
    p.lastPoint=self.lastPoint;
    p.lineColor=self.lineColor;
    p.lineAlpha=self.lineAlpha;
    p.lineWidth=self.lineWidth;
    p.mypath=CGPathCreateCopy(self.mypath);
    p.isSel=self.isSel;
    p.rightdownSel=CGPathCreateCopy(self.rightdownSel);
    p.leftdownSel=CGPathCreateCopy(self.leftdownSel);
    p.rightupSel=CGPathCreateCopy(self.rightupSel);
    p.leftupSel=CGPathCreateCopy(self.leftupSel);
    p.isDel=self.isDel;
    return p;
}

- (void)setInitialPoint:(CGPoint)firstPoint
{
    [self moveToPoint:firstPoint];
    CGPathRelease(self.rightdownSel);
    self.rightdownSel=nil;
    CGPathRelease(self.leftdownSel);
    self.leftdownSel=nil;
    CGPathRelease(self.rightupSel);
    self.rightupSel=nil;
    CGPathRelease(self.leftupSel);
    self.leftupSel=nil;
}

- (void)moveFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint
{
    [self addQuadCurveToPoint:midPoint(endPoint, startPoint) controlPoint:startPoint];
}

- (void)draw
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.lineColor setStroke];
    [self strokeWithBlendMode:kCGBlendModeNormal alpha:self.lineAlpha];
    CGRect rect=[self bounds];
    float width=10;
    if(width<10)
        width=10;
    CGPathRelease(self.mypath);
    self.mypath=CGPathCreateCopyByStrokingPath([self CGPath], NULL, width, kCGLineCapButt, kCGLineJoinRound, 1);
    if(self.isSel) {
        CGPathRelease(self.rightdownSel);
        self.rightdownSel=CGPathCreateWithRect(rect, NULL);
        CGContextSetLineDash(context, 1.0f, NULL, 0);
        CGContextSetLineWidth(context, 2);
        CGContextSetStrokeColorWithColor(context, kColorPointBlue.CGColor);
        CGContextStrokeRect(context, rect);
        //        NSLog(@"xxx %f %f", rect.origin.x, rect.origin.y);
    }
}

-(void)drawselect:(CGContextRef)context Point:(CGPoint)point for:(int)index
{
    return;
}

-(int)isclicked:(CGPoint)point
{
    int sel=0;
    if(CGPathContainsPoint(self.rightdownSel, NULL, point, YES)) {
        sel=1;
    }
    if([self containsPoint:point]) {
        sel=1;
    }
    else if(CGPathContainsPoint(self.mypath, NULL, point, YES)) {
        sel=1;
    }
    self.isSel=sel;
    return self.isSel;
}

-(void)setselect:(int)sel
{
    self.isSel=sel;
    if(self.isSel==0) {
        CGPathRelease(self.rightdownSel);
        self.rightdownSel=nil;
        CGPathRelease(self.leftdownSel);
        self.leftdownSel=nil;
        CGPathRelease(self.rightupSel);
        self.rightupSel=nil;
        CGPathRelease(self.leftupSel);
        self.leftupSel=nil;
    }
    [self draw];
}

- (void)applyTransform
{
    
}

- (void)move:(CGPoint)point to:(CGPoint)point2
{
    float xoff=point2.x-point.x;
    float yoff=point2.y-point.y;
    
    [self applyTransform:CGAffineTransformMakeTranslation(xoff, yoff)];
}

#if !ACE_HAS_ARC

- (void)dealloc
{
    self.lineColor = nil;
    [super dealloc];
}

#endif

@end


#pragma mark - ACEDrawingLineTool

@implementation ACEDrawingLineTool

@synthesize lineColor = _lineColor;
@synthesize lineAlpha = _lineAlpha;
@synthesize lineWidth = _lineWidth;
@synthesize mypath = _mypath;
@synthesize isSel = _isSel;//new add
@synthesize rightdownSel = _rightdownSel;
@synthesize leftdownSel = _leftdownSel;
@synthesize rightupSel = _rightupSel;
@synthesize leftupSel = _leftupSel;
@synthesize firstPoint = _firstPoint;
@synthesize lastPoint = _lastPoint;
@synthesize isDel = _isDel;
@synthesize original = _original;

- (id)Copy
{
    id<ACEDrawingTool> p=[[self class] alloc];
    p.firstPoint=self.firstPoint;
    p.lastPoint=self.lastPoint;
    p.lineColor=self.lineColor;
    p.lineAlpha=self.lineAlpha;
    p.lineWidth=self.lineWidth;
    p.mypath=CGPathCreateCopy(self.mypath);
    p.isSel=self.isSel;
    p.rightdownSel=CGPathCreateCopy(self.rightdownSel);
    p.leftdownSel=CGPathCreateCopy(self.leftdownSel);
    p.rightupSel=CGPathCreateCopy(self.rightupSel);
    p.leftupSel=CGPathCreateCopy(self.leftupSel);
    p.isDel=self.isDel;
    return p;
}

- (void)setInitialPoint:(CGPoint)firstPoint
{
    self.firstPoint = firstPoint;
}

- (void)moveFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint
{
    self.lastPoint = endPoint;
}

- (void)draw
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // set the line properties
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetAlpha(context, self.lineAlpha);
    
    if(self.lastPoint.x==0)
        return;
    // draw the line
    CGContextMoveToPoint(context, self.firstPoint.x, self.firstPoint.y);
    CGContextAddLineToPoint(context, self.lastPoint.x, self.lastPoint.y);
    CGContextStrokePath(context);
    CGPathRelease(self.mypath);
    CGPathRef path=CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, self.firstPoint.x, self.firstPoint.y);
    CGPathAddLineToPoint(path, NULL, self.lastPoint.x, self.lastPoint.y);
    //    float width=self.lineWidth;
    float width = 25;
    if(width<25)
        width=25;
    self.mypath=CGPathCreateCopyByStrokingPath(path, NULL, width, kCGLineCapButt, kCGLineJoinRound, 1);
    CGPathRelease(path);
    if(self.isSel) {
        [self drawselect:context Point:self.firstPoint for:3];
        [self drawselect:context Point:self.lastPoint for:0];
    }
}

-(int)isclicked:(CGPoint)point
{
    int sel=0;
    if(CGPathContainsPoint(self.rightdownSel, NULL, point, YES)) {
        sel=2;
    }
    else if(CGPathContainsPoint(self.leftdownSel, NULL, point, YES)) {
        sel=3;
    }
    else if(CGPathContainsPoint(self.rightupSel, NULL, point, YES)) {
        sel=4;
    }
    else if(CGPathContainsPoint(self.leftupSel, NULL, point, YES)) {
        sel=5;
    }
    else {
        sel=CGPathContainsPoint(self.mypath, NULL, point, YES);
    }
    self.isSel=sel;
    return self.isSel;
}

-(void) drawselect:(CGContextRef)context Point:(CGPoint)point for:(int)index
{
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextSetLineWidth(context, 2);
    //    float width = self.lineWidth;
    float width = 30;
    CGRect rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
    width-=17;
    if(index==0) {
        CGPathRelease(self.rightdownSel);
        self.rightdownSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==1) {
        CGPathRelease(self.leftdownSel);
        self.leftdownSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==2) {
        CGPathRelease(self.rightupSel);
        self.rightupSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==3) {
        CGPathRelease(self.leftupSel);
        self.leftupSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    
    rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
    
    width-=2;
    rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextSetFillColorWithColor(context, kColorPointBlue.CGColor);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
}

-(void)setselect:(int)sel
{
    self.isSel=sel;
    if(self.isSel==0) {
        CGPathRelease(self.rightdownSel);
        self.rightdownSel=nil;
        CGPathRelease(self.leftdownSel);
        self.leftdownSel=nil;
        CGPathRelease(self.rightupSel);
        self.rightupSel=nil;
        CGPathRelease(self.leftupSel);
        self.leftupSel=nil;
    }
    [self draw];
}

- (void)move:(CGPoint)point to:(CGPoint)point2
{
    // 贴右边
    if (self.lastPoint.x + (point2.x - point.x) >= screenW || self.firstPoint.x + (point2.x - point.x) >= screenW)
    {
        if (self.lastPoint.x >= self.firstPoint.x)
        {
            if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
            {
                self.lastPoint = CGPointMake(screenW , screenH - 44);
                self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x) , screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
            }
            else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
            {
                self.lastPoint = CGPointMake(screenW , self.lastPoint.y - self.firstPoint.y + 20 + 44);
                self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x), 20 + 44);
            }
            else
            {
                self.lastPoint = CGPointMake(screenW , self.lastPoint.y + (point2.y - point.y));
                self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x) , self.firstPoint.y + (point2.y - point.y));
            }
        }
        else
        {
            if (self.firstPoint.y + (point2.y - point.y) >= screenH - 44)
            {
                self.firstPoint = CGPointMake(screenW , screenH - 44);
                self.lastPoint = CGPointMake(screenW - (self.firstPoint.x - self.lastPoint.x) , screenH - 44 - (self.firstPoint.y - self.lastPoint.y));
            }
            else if (self.lastPoint.y + (point2.y - point.y) <= 20 + 44)
            {
                self.firstPoint = CGPointMake(screenW , self.firstPoint.y - self.lastPoint.y + 20 + 44);
                self.lastPoint = CGPointMake(screenW - (self.firstPoint.x - self.lastPoint.x), 20 + 44);
            }
            else
            {
                self.firstPoint = CGPointMake(screenW , self.firstPoint.y + (point2.y - point.y));
                self.lastPoint = CGPointMake(screenW - (self.firstPoint.x - self.lastPoint.x) , self.lastPoint.y + (point2.y - point.y));
            }
        }
        
    }
    // 贴左边
    else if ( self.firstPoint.x + (point2.x - point.x) <= 1 || self.lastPoint.x + (point2.x - point.x) <= 1 )
    {
        if (self.lastPoint.x >= self.firstPoint.x)
        {
            if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
            {
                self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , screenH - 44);
                self.firstPoint = CGPointMake(0 , screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
            }
            else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
            {
                self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , self.lastPoint.y - self.firstPoint.y + 20 + 44);
                self.firstPoint = CGPointMake(0 , 20 + 44);
            }
            else
            {
                self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , self.lastPoint.y + (point2.y - point.y));
                self.firstPoint = CGPointMake(0 , self.firstPoint.y + (point2.y - point.y));
            }
        }
        else if (self.lastPoint.x < self.firstPoint.x)
        {
            self.lastPoint = CGPointMake(1 , self.lastPoint.y + (point2.y - point.y));
            self.firstPoint = CGPointMake(self.firstPoint.x , self.firstPoint.y + (point2.y - point.y));
        }
    }
    // 中间
    else
    {
        if (self.lastPoint.y < self.firstPoint.y)
        {
            if (self.firstPoint.y + (point2.y - point.y) >= screenH - 44)
            {
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x) , screenH - 44);
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x), screenH - 44 - (self.firstPoint.y - self.lastPoint.y));
            }
            else if (self.lastPoint.y + (point2.y - point.y) <= 20 + 44)
            {
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x) , self.firstPoint.y - self.lastPoint.y + 20 + 44);
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x), 20 + 44);
            }
            else
            {
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), self.firstPoint.y + (point2.y - point.y));
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x), self.lastPoint.y + (point2.y - point.y));
            }
        }
        else if (self.lastPoint.y >= self.firstPoint.y)
        {
            if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
            {
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , screenH - 44);
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
            }
            else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
            {
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , self.lastPoint.y - self.firstPoint.y + 20 + 44);
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), 20 + 44);
            }
            else
            {
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x), self.lastPoint.y + (point2.y - point.y));
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), self.firstPoint.y + (point2.y - point.y));
            }
        }
    }
}

//- (void)move:(CGPoint)point to:(CGPoint)point2
//{
//    //    if (self.lastPoint.x < self.firstPoint.x)
//    //    {
//    //        self.lastPoint  = CGPointMake(0-self.lastPoint.x, self.lastPoint.y);
//    //        self.firstPoint = CGPointMake(0-self.firstPoint.x, self.firstPoint.y);
//    //    }
//    //
//    //    if (self.lastPoint.y < self.firstPoint.y)
//    //    {
//    //        self.lastPoint  = CGPointMake(self.lastPoint.x, 0-self.lastPoint.y);
//    //        self.firstPoint = CGPointMake(self.firstPoint.x, 0-self.firstPoint.y);
//    //    }
//    
//    // 贴右边
//    if (self.lastPoint.x + (point2.x - point.x) >= screenW)
//    {
//        if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
//        {
//            self.lastPoint = CGPointMake(screenW , screenH - 44);
//            self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x) , screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
//        }
//        else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
//        {
//            self.lastPoint = CGPointMake(screenW , self.lastPoint.y - self.firstPoint.y + 20 + 44);
//            self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x), 20 + 44);
//        }
//        else
//        {
//            self.lastPoint = CGPointMake(screenW , self.lastPoint.y + (point2.y - point.y));
//            self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x) , self.firstPoint.y + (point2.y - point.y));
//        }
//    }
//    // 贴左边
//    else if (self.firstPoint.x + (point2.x - point.x) <= 0)
//    {
//        if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
//        {
//            self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , screenH - 44);
//            self.firstPoint = CGPointMake(0 , screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
//        }
//        else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
//        {
//            self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , self.lastPoint.y - self.firstPoint.y + 20 + 44);
//            self.firstPoint = CGPointMake(0 , 20 + 44);
//        }
//        else
//        {
//            self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , self.lastPoint.y + (point2.y - point.y));
//            self.firstPoint = CGPointMake(0 , self.firstPoint.y + (point2.y - point.y));
//        }
//        
//    }
//    // 中间
//    else
//    {
//        if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
//        {
//            self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , screenH - 44);
//            self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
//        }
//        else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
//        {
//            self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , self.lastPoint.y - self.firstPoint.y + 20 + 44);
//            self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), 20 + 44);
//        }
//        else
//        {
//            self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x), self.lastPoint.y + (point2.y - point.y));
//            self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), self.firstPoint.y + (point2.y - point.y));
//        }
//        
//    }
//}
//- (void)move:(CGPoint)point to:(CGPoint)point2
//{
//    self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , self.lastPoint.y + (point2.y - point.y));
//    self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x) , self.firstPoint.y + (point2.y - point.y));
//}

-(void)resizerightdown:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.lastPoint=CGPointMake(self.lastPoint.x+xoff, self.lastPoint.y+yoff);
}

-(void)resizeleftup:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.firstPoint=CGPointMake(self.firstPoint.x+xoff, self.firstPoint.y+yoff);
}


#if !ACE_HAS_ARC

- (void)dealloc
{
    self.lineColor = nil;
    [super dealloc];
}

#endif

@end


#pragma mark - ACEDrawingArrowTool

@implementation ACEDrawingArrowTool

@synthesize lineColor = _lineColor;
@synthesize lineAlpha = _lineAlpha;
@synthesize lineWidth = _lineWidth;
@synthesize mypath = _mypath;
@synthesize isSel = _isSel;//new add
@synthesize rightdownSel = _rightdownSel;
@synthesize leftdownSel = _leftdownSel;
@synthesize rightupSel = _rightupSel;
@synthesize leftupSel = _leftupSel;
@synthesize firstPoint = _firstPoint;
@synthesize lastPoint = _lastPoint;
@synthesize isDel = _isDel;
@synthesize original = _original;

- (id)Copy
{
    id<ACEDrawingTool> p=[[self class] alloc];
    p.firstPoint=self.firstPoint;
    p.lastPoint=self.lastPoint;
    p.lineColor=self.lineColor;
    p.lineAlpha=self.lineAlpha;
    p.lineWidth=self.lineWidth;
    p.mypath=CGPathCreateCopy(self.mypath);
    p.isSel=self.isSel;
    p.rightdownSel=CGPathCreateCopy(self.rightdownSel);
    p.leftdownSel=CGPathCreateCopy(self.leftdownSel);
    p.rightupSel=CGPathCreateCopy(self.rightupSel);
    p.leftupSel=CGPathCreateCopy(self.leftupSel);
    p.isDel=self.isDel;
    return p;
}

- (void)setInitialPoint:(CGPoint)firstPoint
{
    self.firstPoint = firstPoint;
}

- (void)moveFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint
{
    self.lastPoint = endPoint;
}

- (void)draw
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // set the line properties
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetAlpha(context, self.lineAlpha);
    
    if(self.lastPoint.x==0)
        return;
    // draw the line
    CGContextMoveToPoint(context, self.firstPoint.x, self.firstPoint.y);
    CGContextAddLineToPoint(context, self.lastPoint.x, self.lastPoint.y);
    float sinA=(self.firstPoint.y-self.lastPoint.y)/sqrt((self.firstPoint.x-self.lastPoint.x)*(self.firstPoint.x-self.lastPoint.x)+(self.firstPoint.y-self.lastPoint.y)*(self.firstPoint.y-self.lastPoint.y));
    float cosA=(self.firstPoint.x-self.lastPoint.x)/sqrt((self.firstPoint.x-self.lastPoint.x)*(self.firstPoint.x-self.lastPoint.x)+(self.firstPoint.y-self.lastPoint.y)*(self.firstPoint.y-self.lastPoint.y));
    float sinB=cos(3.14/6)*sinA+sin(3.14/6)*cosA;
    float cosB=cos(3.14/6)*cosA-sin(3.14/6)*sinA;
    float sinC=cos(3.14/6)*sinA-sin(3.14/6)*cosA;
    float cosC=cos(3.14/6)*cosA+sin(3.14/6)*sinA;
    float pbx=self.lastPoint.x+8*cosB,pby=self.lastPoint.y+8*sinB,pcx=self.lastPoint.x+8*cosC,pcy=self.lastPoint.y+8*sinC;
    CGContextMoveToPoint(context, self.lastPoint.x, self.lastPoint.y);
    CGContextAddLineToPoint(context, pbx, pby);
    CGContextMoveToPoint(context, self.lastPoint.x, self.lastPoint.y);
    CGContextAddLineToPoint(context, pcx, pcy);
    CGContextStrokePath(context);
    CGPathRelease(self.mypath);
    CGPathRef path=CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, self.firstPoint.x, self.firstPoint.y);
    CGPathAddLineToPoint(path, NULL, self.lastPoint.x, self.lastPoint.y);
    //    float width=self.lineWidth;
    float width = 25;
    if(width<25)
        width=25;
    self.mypath=CGPathCreateCopyByStrokingPath(path, NULL, width, kCGLineCapButt, kCGLineJoinRound, 1);
    CGPathRelease(path);
    if(self.isSel) {
        [self drawselect:context Point:self.firstPoint for:3];
        [self drawselect:context Point:self.lastPoint for:0];
    }
}

-(int)isclicked:(CGPoint)point
{
    int sel=0;
    if(CGPathContainsPoint(self.rightdownSel, NULL, point, YES)) {
        sel=2;
    }
    else if(CGPathContainsPoint(self.leftdownSel, NULL, point, YES)) {
        sel=3;
    }
    else if(CGPathContainsPoint(self.rightupSel, NULL, point, YES)) {
        sel=4;
    }
    else if(CGPathContainsPoint(self.leftupSel, NULL, point, YES)) {
        sel=5;
    }
    else {
        sel=CGPathContainsPoint(self.mypath, NULL, point, YES);
    }
    self.isSel=sel;
    return self.isSel;
}

-(void) drawselect:(CGContextRef)context Point:(CGPoint)point for:(int)index
{
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextSetLineWidth(context, 2);
    //    float width = self.lineWidth;
    float width = 30;
    CGRect rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
    width-=17;
    if(index==0) {
        CGPathRelease(self.rightdownSel);
        self.rightdownSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==1) {
        CGPathRelease(self.leftdownSel);
        self.leftdownSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==2) {
        CGPathRelease(self.rightupSel);
        self.rightupSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==3) {
        CGPathRelease(self.leftupSel);
        self.leftupSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    
    rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
    
    width-=2;
    rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextSetFillColorWithColor(context, kColorPointBlue.CGColor);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
}

-(void)setselect:(int)sel
{
    self.isSel=sel;
    if(self.isSel==0) {
        CGPathRelease(self.rightdownSel);
        self.rightdownSel=nil;
        CGPathRelease(self.leftdownSel);
        self.leftdownSel=nil;
        CGPathRelease(self.rightupSel);
        self.rightupSel=nil;
        CGPathRelease(self.leftupSel);
        self.leftupSel=nil;
    }
    [self draw];
}

- (void)move:(CGPoint)point to:(CGPoint)point2
{
    // 贴右边
    if (self.lastPoint.x + (point2.x - point.x) >= screenW || self.firstPoint.x + (point2.x - point.x) >= screenW)
    {
        if (self.lastPoint.x >= self.firstPoint.x)
        {
            if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
            {
                self.lastPoint = CGPointMake(screenW , screenH - 44);
                self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x) , screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
            }
            else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
            {
                self.lastPoint = CGPointMake(screenW , self.lastPoint.y - self.firstPoint.y + 20 + 44);
                self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x), 20 + 44);
            }
            else
            {
                self.lastPoint = CGPointMake(screenW , self.lastPoint.y + (point2.y - point.y));
                self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x) , self.firstPoint.y + (point2.y - point.y));
            }
        }
        else
        {
            if (self.firstPoint.y + (point2.y - point.y) >= screenH - 44)
            {
                self.firstPoint = CGPointMake(screenW , screenH - 44);
                self.lastPoint = CGPointMake(screenW - (self.firstPoint.x - self.lastPoint.x) , screenH - 44 - (self.firstPoint.y - self.lastPoint.y));
            }
            else if (self.lastPoint.y + (point2.y - point.y) <= 20 + 44)
            {
                self.firstPoint = CGPointMake(screenW , self.firstPoint.y - self.lastPoint.y + 20 + 44);
                self.lastPoint = CGPointMake(screenW - (self.firstPoint.x - self.lastPoint.x), 20 + 44);
            }
            else
            {
                self.firstPoint = CGPointMake(screenW , self.firstPoint.y + (point2.y - point.y));
                self.lastPoint = CGPointMake(screenW - (self.firstPoint.x - self.lastPoint.x) , self.lastPoint.y + (point2.y - point.y));
            }
        }
        
    }
    // 贴左边
    else if ( self.firstPoint.x + (point2.x - point.x) <= 1 || self.lastPoint.x + (point2.x - point.x) <= 1 )
    {
        if (self.lastPoint.x >= self.firstPoint.x)
        {
            if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
            {
                self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , screenH - 44);
                self.firstPoint = CGPointMake(0 , screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
            }
            else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
            {
                self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , self.lastPoint.y - self.firstPoint.y + 20 + 44);
                self.firstPoint = CGPointMake(0 , 20 + 44);
            }
            else
            {
                self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , self.lastPoint.y + (point2.y - point.y));
                self.firstPoint = CGPointMake(0 , self.firstPoint.y + (point2.y - point.y));
            }
        }
        else if (self.lastPoint.x < self.firstPoint.x)
        {
            self.lastPoint = CGPointMake(1 , self.lastPoint.y + (point2.y - point.y));
            self.firstPoint = CGPointMake(self.firstPoint.x , self.firstPoint.y + (point2.y - point.y));
        }
    }
    // 中间
    else
    {
        if (self.lastPoint.y < self.firstPoint.y)
        {
            if (self.firstPoint.y + (point2.y - point.y) >= screenH - 44)
            {
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x) , screenH - 44);
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x), screenH - 44 - (self.firstPoint.y - self.lastPoint.y));
            }
            else if (self.lastPoint.y + (point2.y - point.y) <= 20 + 44)
            {
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x) , self.firstPoint.y - self.lastPoint.y + 20 + 44);
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x), 20 + 44);
            }
            else
            {
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), self.firstPoint.y + (point2.y - point.y));
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x), self.lastPoint.y + (point2.y - point.y));
            }
        }
        else if (self.lastPoint.y >= self.firstPoint.y)
        {
            if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
            {
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , screenH - 44);
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
            }
            else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
            {
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , self.lastPoint.y - self.firstPoint.y + 20 + 44);
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), 20 + 44);
            }
            else
            {
                self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x), self.lastPoint.y + (point2.y - point.y));
                self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), self.firstPoint.y + (point2.y - point.y));
            }
        }
    }
}





//- (void)move:(CGPoint)point to:(CGPoint)point2
//{
//    self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , self.lastPoint.y + (point2.y - point.y));
//    self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x) , self.firstPoint.y + (point2.y - point.y));
//}

-(void)resizerightdown:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.lastPoint=CGPointMake(self.lastPoint.x+xoff, self.lastPoint.y+yoff);
}

-(void)resizeleftup:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.firstPoint=CGPointMake(self.firstPoint.x+xoff, self.firstPoint.y+yoff);
}


#if !ACE_HAS_ARC

- (void)dealloc
{
    self.lineColor = nil;
    [super dealloc];
}

#endif

@end

#pragma mark - ACEDrawingRectangleTool

@implementation ACEDrawingRectangleTool

@synthesize lineColor = _lineColor;
@synthesize lineAlpha = _lineAlpha;
@synthesize lineWidth = _lineWidth;
@synthesize mypath = _mypath;
@synthesize isSel = _isSel;//new add
@synthesize rightdownSel = _rightdownSel;
@synthesize leftdownSel = _leftdownSel;
@synthesize rightupSel = _rightupSel;
@synthesize leftupSel = _leftupSel;
@synthesize firstPoint = _firstPoint;
@synthesize lastPoint = _lastPoint;
@synthesize isDel = _isDel;
@synthesize original = _original;

- (id)Copy
{
    id<ACEDrawingTool> p=[[self class] alloc];
    p.firstPoint=self.firstPoint;
    p.lastPoint=self.lastPoint;
    p.lineColor=self.lineColor;
    p.lineAlpha=self.lineAlpha;
    p.lineWidth=self.lineWidth;
    p.mypath=CGPathCreateCopy(self.mypath);
    p.isSel=self.isSel;
    p.rightdownSel=CGPathCreateCopy(self.rightdownSel);
    p.leftdownSel=CGPathCreateCopy(self.leftdownSel);
    p.rightupSel=CGPathCreateCopy(self.rightupSel);
    p.leftupSel=CGPathCreateCopy(self.leftupSel);
    p.isDel=self.isDel;
    return p;
}

- (void)setInitialPoint:(CGPoint)firstPoint
{
    self.firstPoint = firstPoint;
}

- (void)moveFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint
{
    self.lastPoint = endPoint;
}

- (void)draw
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // set the properties
    CGContextSetAlpha(context, self.lineAlpha);
    
    // draw the rectangle
    CGRect rectToFill = CGRectMake(self.firstPoint.x, self.firstPoint.y, self.lastPoint.x - self.firstPoint.x, self.lastPoint.y - self.firstPoint.y);
    if(self.lastPoint.x==0)
        return;
    if (self.fill) {
        CGContextSetFillColorWithColor(context, self.lineColor.CGColor);
        CGContextFillRect(UIGraphicsGetCurrentContext(), rectToFill);
    } else {
        CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
        CGContextSetLineWidth(context, self.lineWidth);
        CGContextStrokeRect(UIGraphicsGetCurrentContext(), rectToFill);
    }
    float minx,miny,maxx,maxy;
    minx=MIN(self.firstPoint.x, self.lastPoint.x);
    miny=MIN(self.firstPoint.y, self.lastPoint.y);
    maxx=MAX(self.firstPoint.x, self.lastPoint.x);
    maxy=MAX(self.firstPoint.y, self.lastPoint.y);
    CGPathRelease(self.mypath);
    if(self.fill==NO) {
        float width=25;
        if(width<25)
            width=25;
        self.mypath=CGPathCreateCopyByStrokingPath(CGPathCreateWithRect(rectToFill, NULL), NULL, width, kCGLineCapButt, kCGLineJoinRound, 1);
    }
    else {
        self.mypath=CGPathCreateWithRect(rectToFill, NULL);
    }
    if(self.isSel) {
        [self drawselect:context Point:CGPointMake(minx, miny) for:3];
        [self drawselect:context Point:CGPointMake(minx, maxy) for:1];
        [self drawselect:context Point:CGPointMake(maxx, maxy) for:0];
        [self drawselect:context Point:CGPointMake(maxx, miny) for:2];
    }
}

-(int)isclicked:(CGPoint)point
{
    int sel=0;
    if(CGPathContainsPoint(self.rightdownSel, NULL, point, self.fill)) {
        sel=2;
    }
    else if(CGPathContainsPoint(self.leftdownSel, NULL, point, self.fill)) {
        sel=3;
    }
    else if(CGPathContainsPoint(self.rightupSel, NULL, point, self.fill)) {
        sel=4;
    }
    else if(CGPathContainsPoint(self.leftupSel, NULL, point, self.fill)) {
        sel=5;
    }
    else {
        sel=CGPathContainsPoint(self.mypath, NULL, point, self.fill);
    }
    self.isSel=sel;
    float minx,miny,maxx,maxy;
    minx=MIN(self.firstPoint.x, self.lastPoint.x);
    miny=MIN(self.firstPoint.y, self.lastPoint.y);
    maxx=MAX(self.firstPoint.x, self.lastPoint.x);
    maxy=MAX(self.firstPoint.y, self.lastPoint.y);
    self.firstPoint=CGPointMake(minx, miny);
    self.lastPoint=CGPointMake(maxx, maxy);
    return self.isSel;
}

-(void) drawselect:(CGContextRef)context Point:(CGPoint)point for:(int)index
{
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextSetLineWidth(context, 2);
    //    float width = self.lineWidth;
    float width = 30;
    CGRect rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
    width-=17;
    if(index==0) {
        CGPathRelease(self.rightdownSel);
        self.rightdownSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==1) {
        CGPathRelease(self.leftdownSel);
        self.leftdownSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==2) {
        CGPathRelease(self.rightupSel);
        self.rightupSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==3) {
        CGPathRelease(self.leftupSel);
        self.leftupSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    
    rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
    
    width-=2;
    rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextSetFillColorWithColor(context, kColorPointBlue.CGColor);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
}


-(void)setselect:(int)sel
{
    self.isSel=sel;
    if(self.isSel==0) {
        CGPathRelease(self.rightdownSel);
        self.rightdownSel=nil;
        CGPathRelease(self.leftdownSel);
        self.leftdownSel=nil;
        CGPathRelease(self.rightupSel);
        self.rightupSel=nil;
        CGPathRelease(self.leftupSel);
        self.leftupSel=nil;
    }
    [self draw];
}

- (void)move:(CGPoint)point to:(CGPoint)point2
{
    //    NSLog(@"ppppp %f %f", self.firstPoint.x, self.lastPoint.x);
    
    // 贴右边
    if (self.lastPoint.x + (point2.x - point.x) >= screenW)
    {
        if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
        {
            self.lastPoint = CGPointMake(screenW , screenH - 44);
            self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x) , screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
        }
        else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
        {
            self.lastPoint = CGPointMake(screenW , self.lastPoint.y - self.firstPoint.y + 20 + 44);
            self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x), 20 + 44);
        }
        else
        {
            self.lastPoint = CGPointMake(screenW , self.lastPoint.y + (point2.y - point.y));
            self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x) , self.firstPoint.y + (point2.y - point.y));
        }
    }
    // 贴左边
    else if (self.firstPoint.x + (point2.x - point.x) <= 0)
    {
        if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
        {
            self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , screenH - 44);
            self.firstPoint = CGPointMake(0 , screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
        }
        else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
        {
            self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , self.lastPoint.y - self.firstPoint.y + 20 + 44);
            self.firstPoint = CGPointMake(0 , 20 + 44);
        }
        else
        {
            self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , self.lastPoint.y + (point2.y - point.y));
            self.firstPoint = CGPointMake(0 , self.firstPoint.y + (point2.y - point.y));
        }
        
    }
    // 中间
    else
    {
        if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
        {
            self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , screenH - 44);
            self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
        }
        else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
        {
            self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , self.lastPoint.y - self.firstPoint.y + 20 + 44);
            self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), 20 + 44);
        }
        else
        {
            self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x), self.lastPoint.y + (point2.y - point.y));
            self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), self.firstPoint.y + (point2.y - point.y));
        }
        
    }
}
//- (void)move:(CGPoint)point to:(CGPoint)point2
//{
//    self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , self.lastPoint.y + (point2.y - point.y));
//    self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x) , self.firstPoint.y + (point2.y - point.y));
//}

-(void)resizerightdown:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.lastPoint=CGPointMake(self.lastPoint.x+xoff, self.lastPoint.y+yoff);
}

-(void)resizeleftdown:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.firstPoint=CGPointMake(self.firstPoint.x+xoff, self.firstPoint.y);
    self.lastPoint=CGPointMake(self.lastPoint.x, self.lastPoint.y+yoff);
}
-(void)resizerightup:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.firstPoint=CGPointMake(self.firstPoint.x, self.firstPoint.y+yoff);
    self.lastPoint=CGPointMake(self.lastPoint.x+xoff, self.lastPoint.y);
}

-(void)resizeleftup:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.firstPoint=CGPointMake(self.firstPoint.x+xoff, self.firstPoint.y+yoff);
}

#if !ACE_HAS_ARC

- (void)dealloc
{
    self.lineColor = nil;
    [super dealloc];
}

#endif

@end

#pragma mark - ACEDrawingEllipseTool

@implementation ACEDrawingEllipseTool

@synthesize lineColor = _lineColor;
@synthesize lineAlpha = _lineAlpha;
@synthesize lineWidth = _lineWidth;
@synthesize mypath = _mypath;
@synthesize isSel = _isSel;//new add
@synthesize rightdownSel = _rightdownSel;
@synthesize leftdownSel = _leftdownSel;
@synthesize rightupSel = _rightupSel;
@synthesize leftupSel = _leftupSel;
@synthesize firstPoint = _firstPoint;
@synthesize lastPoint = _lastPoint;
@synthesize isDel = _isDel;
@synthesize original = _original;

- (id)Copy
{
    id<ACEDrawingTool> p=[[self class] alloc];
    p.firstPoint=self.firstPoint;
    p.lastPoint=self.lastPoint;
    p.lineColor=self.lineColor;
    p.lineAlpha=self.lineAlpha;
    p.lineWidth=self.lineWidth;
    p.mypath=CGPathCreateCopy(self.mypath);
    p.isSel=self.isSel;
    p.rightdownSel=CGPathCreateCopy(self.rightdownSel);
    p.leftdownSel=CGPathCreateCopy(self.leftdownSel);
    p.rightupSel=CGPathCreateCopy(self.rightupSel);
    p.leftupSel=CGPathCreateCopy(self.leftupSel);
    p.isDel=self.isDel;
    return p;
}

- (void)setInitialPoint:(CGPoint)firstPoint
{
    self.firstPoint = firstPoint;
}

- (void)moveFromPoint:(CGPoint)startPoint toPoint:(CGPoint)endPoint
{
    self.lastPoint = endPoint;
}

- (void)draw
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // set the properties
    CGContextSetAlpha(context, self.lineAlpha);
    
    // draw the ellipse
    CGRect rectToFill = CGRectMake(self.firstPoint.x, self.firstPoint.y, self.lastPoint.x - self.firstPoint.x, self.lastPoint.y - self.firstPoint.y);
    if(self.lastPoint.x==0)
        return;
    if (self.fill) {
        CGContextSetFillColorWithColor(context, self.lineColor.CGColor);
        CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rectToFill);
        
    } else {
        CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
        CGContextSetLineWidth(context, self.lineWidth);
        CGContextStrokeEllipseInRect(UIGraphicsGetCurrentContext(), rectToFill);
    }
    float minx,miny,maxx,maxy;
    minx=MIN(self.firstPoint.x, self.lastPoint.x);
    miny=MIN(self.firstPoint.y, self.lastPoint.y);
    maxx=MAX(self.firstPoint.x, self.lastPoint.x);
    maxy=MAX(self.firstPoint.y, self.lastPoint.y);
    CGPathRelease(self.mypath);
    if(self.fill==NO) {
        float width=25;
        if(width<25)
            width=25;
        self.mypath=CGPathCreateCopyByStrokingPath(CGPathCreateWithRect(rectToFill, NULL), NULL, width, kCGLineCapButt, kCGLineJoinRound, 1);
    }
    else {
        self.mypath=CGPathCreateWithRect(rectToFill, NULL);
    }
    if(self.isSel) {
        [self drawselect:context Point:CGPointMake(minx, miny) for:3];
        [self drawselect:context Point:CGPointMake(minx, maxy) for:1];
        [self drawselect:context Point:CGPointMake(maxx, maxy) for:0];
        [self drawselect:context Point:CGPointMake(maxx, miny) for:2];
    }
}

-(int)isclicked:(CGPoint)point
{
    int sel=0;
    if(CGPathContainsPoint(self.rightdownSel, NULL, point, self.fill)) {
        sel=2;
    }
    else if(CGPathContainsPoint(self.leftdownSel, NULL, point, self.fill)) {
        sel=3;
    }
    else if(CGPathContainsPoint(self.rightupSel, NULL, point, self.fill)) {
        sel=4;
    }
    else if(CGPathContainsPoint(self.leftupSel, NULL, point, self.fill)) {
        sel=5;
    }
    else {
        sel=CGPathContainsPoint(self.mypath, NULL, point, self.fill);
    }
    self.isSel=sel;
    float minx,miny,maxx,maxy;
    minx=MIN(self.firstPoint.x, self.lastPoint.x);
    miny=MIN(self.firstPoint.y, self.lastPoint.y);
    maxx=MAX(self.firstPoint.x, self.lastPoint.x);
    maxy=MAX(self.firstPoint.y, self.lastPoint.y);
    self.firstPoint=CGPointMake(minx, miny);
    self.lastPoint=CGPointMake(maxx, maxy);
    return self.isSel;
}

-(void) drawselect:(CGContextRef)context Point:(CGPoint)point for:(int)index
{
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextSetLineWidth(context, 2);
    //    float width = self.lineWidth;
    float width = 30;
    CGRect rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
    width-=17;
    if(index==0) {
        CGPathRelease(self.rightdownSel);
        self.rightdownSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==1) {
        CGPathRelease(self.leftdownSel);
        self.leftdownSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==2) {
        CGPathRelease(self.rightupSel);
        self.rightupSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    else if(index==3) {
        CGPathRelease(self.leftupSel);
        self.leftupSel=CGPathCreateWithEllipseInRect(rect, NULL);
    }
    
    rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
    
    width-=2;
    rect = CGRectMake(point.x-width/2, point.y-width/2, width, width);
    CGContextSetFillColorWithColor(context, kColorPointBlue.CGColor);
    CGContextFillEllipseInRect(UIGraphicsGetCurrentContext(), rect);
}

-(void)setselect:(int)sel
{
    self.isSel=sel;
    if(self.isSel==0) {
        CGPathRelease(self.rightdownSel);
        self.rightdownSel=nil;
        CGPathRelease(self.leftdownSel);
        self.leftdownSel=nil;
        CGPathRelease(self.rightupSel);
        self.rightupSel=nil;
        CGPathRelease(self.leftupSel);
        self.leftupSel=nil;
    }
    [self draw];
}

- (void)move:(CGPoint)point to:(CGPoint)point2
{
    //    NSLog(@"ppppp %f %f", self.firstPoint.x, self.lastPoint.x);
    
    // 贴右边
    if (self.lastPoint.x + (point2.x - point.x) >= screenW)
    {
        if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
        {
            self.lastPoint = CGPointMake(screenW , screenH - 44);
            self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x) , screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
        }
        else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
        {
            self.lastPoint = CGPointMake(screenW , self.lastPoint.y - self.firstPoint.y + 20 + 44);
            self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x), 20 + 44);
        }
        else
        {
            self.lastPoint = CGPointMake(screenW , self.lastPoint.y + (point2.y - point.y));
            self.firstPoint = CGPointMake(screenW - (self.lastPoint.x - self.firstPoint.x) , self.firstPoint.y + (point2.y - point.y));
        }
    }
    // 贴左边
    else if (self.firstPoint.x + (point2.x - point.x) <= 0)
    {
        if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
        {
            self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , screenH - 44);
            self.firstPoint = CGPointMake(0 , screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
        }
        else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
        {
            self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , self.lastPoint.y - self.firstPoint.y + 20 + 44);
            self.firstPoint = CGPointMake(0 , 20 + 44);
        }
        else
        {
            self.lastPoint = CGPointMake(self.lastPoint.x - self.firstPoint.x , self.lastPoint.y + (point2.y - point.y));
            self.firstPoint = CGPointMake(0 , self.firstPoint.y + (point2.y - point.y));
        }
        
    }
    // 中间
    else
    {
        if (self.lastPoint.y + (point2.y - point.y) >= screenH - 44)
        {
            self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , screenH - 44);
            self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), screenH - 44 - (self.lastPoint.y - self.firstPoint.y));
        }
        else if (self.firstPoint.y + (point2.y - point.y) <= 20 + 44)
        {
            self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x) , self.lastPoint.y - self.firstPoint.y + 20 + 44);
            self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), 20 + 44);
        }
        else
        {
            self.lastPoint = CGPointMake(self.lastPoint.x + (point2.x - point.x), self.lastPoint.y + (point2.y - point.y));
            self.firstPoint = CGPointMake(self.firstPoint.x + (point2.x - point.x), self.firstPoint.y + (point2.y - point.y));
        }
        
    }
}

-(void)resizerightdown:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.lastPoint=CGPointMake(self.lastPoint.x+xoff, self.lastPoint.y+yoff);
}

-(void)resizeleftdown:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.firstPoint=CGPointMake(self.firstPoint.x+xoff, self.firstPoint.y);
    self.lastPoint=CGPointMake(self.lastPoint.x, self.lastPoint.y+yoff);
}
-(void)resizerightup:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.firstPoint=CGPointMake(self.firstPoint.x, self.firstPoint.y+yoff);
    self.lastPoint=CGPointMake(self.lastPoint.x+xoff, self.lastPoint.y);
}

-(void)resizeleftup:(CGPoint)point1 to:(CGPoint)point2
{
    float xoff=point2.x-point1.x;
    float yoff=point2.y-point1.y;
    self.firstPoint=CGPointMake(self.firstPoint.x+xoff, self.firstPoint.y+yoff);
}

#if !ACE_HAS_ARC

- (void)dealloc
{
    self.lineColor = nil;
    [super dealloc];
}

#endif

@end
